package container

import (
	_ "com.yy.api/common/logger"
	_ "com.yy.api/server/config"
	_ "com.yy.api/server/lib/redis"
	_ "com.yy.api/server/models/mapper"
	_ "fmt"
	_ "slices"
	_ "strings"
)

//var dnConn = config.GetDBConnection()
//var imageMapper = mapper.NewImageMapper(dnConn)
//var containerMapper = mapper.NewContainerMapper(dnConn)
//var usedHostPortListKey = redis.GetUsedHostPortListKey()
//var rowImageListKey = redis.GetRowImageListKey()
//var redisOperator = redis.GetRedisOperator()
//
//func checkUsedPorts(hostPortList []string) error {
//	// 从redis中获取 已占用的端口列表
//	isOccupied := true
//	var usedHostPortList string
//	var err error
//	usedHostPortList, err = redisOperator.GetStrValue(usedHostPortListKey)
//	if err != nil {
//		logger.Default.Errorf("Get Used Host Port List from DB failed: %v", err)
//		usedHostPortList, err = containerMapper.ListHostPortMappings()
//		if err != nil {
//			logger.Default.Errorf("Get Used Host Port List from DB failed: %v", err)
//			return err
//		}
//		if err := redisOperator.ResetStrKV(usedHostPortListKey, usedHostPortList); err != nil {
//			logger.Default.Errorf("Reset Used Host Port List to Redis failed: %v", err)
//		}
//	}
//	for _, hostPort := range hostPortList {
//		if !slices.Contains(strings.Split(usedHostPortList, ","), hostPort) {
//			isOccupied = false
//		} else {
//			isOccupied = true
//		}
//	}
//	// 检查端口是否被占用
//	if isOccupied {
//		return fmt.Errorf("宿主机端口已被占用，请修改端口后重试")
//	}
//	return nil
//}

//func checkLocalImage(rowImage string) error {
//	isExists := true
//	// 从redis中获取本地镜像列表
//	var localImageList string
//	var err error
//	localImageList, err = redisOperator.GetStrValue(rowImageListKey)
//	if err != nil {
//		logger.Default.Errorf("Get Row Image List from DB failed: %v", err)
//		localImageList, err = imageMapper.ListRowImage()
//		if err != nil {
//			logger.Default.Errorf("Get Row Image List from DB failed: %v", err)
//			return err
//		}
//		if err := redisOperator.ResetStrKV(rowImageListKey, localImageList); err != nil {
//			logger.Default.Errorf("Reset Row Image List to Redis failed: %v", err)
//		}
//	}
//	if !slices.Contains(strings.Split(localImageList, ","), rowImage) {
//		isExists = false
//	}
//	if !isExists {
//		return fmt.Errorf("镜像不存在，请先拉取镜像")
//	}
//	return nil
//}
//func addUsedHostPortToRedisList(hostPortList []string) {
//	// 获取redis中的已占用端口列表
//	oldUsedHostPortList, err := redisOperator.GetStrValue(usedHostPortListKey)
//	if err != nil {
//		oldUsedHostPortList, err = containerMapper.ListHostPortMappings()
//		if err != nil {
//			logger.Default.Errorf("Get Used Host Port List from DB failed: %v", err)
//			return
//		}
//		if err := redisOperator.ResetStrKV(usedHostPortListKey, oldUsedHostPortList); err != nil {
//			logger.Default.Errorf("Reset Used Host Port List to Redis failed: %v", err)
//		}
//		return
//	}
//	var newUsedHostPortList []string
//	newUsedHostPortList = append(hostPortList, oldUsedHostPortList)
//	if err := redisOperator.SetStrValue(usedHostPortListKey, strings.Join(newUsedHostPortList, ",")); err != nil {
//		logger.Default.Errorf("Set Used Host Port List to Redis failed: %v", err)
//	}
//}
//
//func addRowImageToRedisList(rowImage string) {
//	// 获取redis中的镜像列表
//	oldRowImageList, err := redisOperator.GetStrValue(rowImageListKey)
//	if err != nil {
//		logger.Default.Errorf("Get Row Image List from Redis failed,Trying Get Row Image List From DB: %v", err)
//		oldRowImageList, err = imageMapper.ListRowImage()
//		if err != nil {
//			logger.Default.Errorf("Get Row Image List from DB failed: %v", err)
//		}
//		if err := redisOperator.SetStrValue(rowImageListKey, oldRowImageList); err != nil {
//			logger.Default.Errorf("Reset Row Image List to Redis failed: %v", err)
//		}
//	}
//	newRowImageList := []string{oldRowImageList, rowImage}
//	if err := redisOperator.SetStrValue(rowImageListKey, strings.Join(newRowImageList, ",")); err != nil {
//		logger.Default.Errorf("Set Row Image List to Redis failed: %v", err)
//	}
//}
